package com.liveneo.system.base.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import com.liveneo.adapter.base.entity.PrpmLossAccessory;
import com.liveneo.adapter.base.entity.PrpmLossFit;
import com.liveneo.adapter.base.entity.PrpmLossMain;
import com.liveneo.adapter.base.entity.PrpmLossRepair;
import com.liveneo.adapter.mobile.model.dto.CaseInfoDto;
import com.liveneo.adapter.mobile.model.dto.ClaimLossMainDto;
import com.liveneo.adapter.mobile.model.dto.PlatLossSingleDto;
import com.liveneo.adapter.mobile.model.dto.PolicyDto;
import com.liveneo.adapter.mobile.model.dto.PolicyItemDto;
import com.liveneo.adapter.mobile.service.LossItemService;
import com.liveneo.adapter.webservice.client.claim.EvaluateInfoResponseDto;
import com.liveneo.adapter.webservice.server.claim.CaseInfo;
import com.liveneo.adapter.webservice.server.claim.EstimateInfo;
import com.liveneo.adapter.webservice.server.claim.GcEvaluateAccessory;
import com.liveneo.adapter.webservice.server.claim.GcEvaluateFit;
import com.liveneo.adapter.webservice.server.claim.GcEvaluateRepair;
import com.liveneo.adapter.webservice.server.claim.PolicyInfo;
import com.liveneo.adapter.webservice.server.claim.model.Claim2VsResponse;

/**
 * @Title:封装对象转换类
 * @Description:
 * @Author:zhoupan
 * @Since:2015-8-14 下午01:46:14
 * @Version:1.0.0
 * @Copyright (c) 北京朗泰恒盛信息技术有限公司-版权所有
 */
public class TransModelUtil {

    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    LossItemService lossItemService = (LossItemService) wac.getBean("lossItemServiceImpl");

    /**
     * 
     * @Description:获取定损单信息，将理赔对象转成LossSingleDto
     * @param claim2VsResponse
     * @return
     * @throws Exception
     * @ReturnType:LossSingleDto
     * @exception
     * @Since:2015-8-14 下午03:11:18
     * @Author:zhoupan
     */
    public PlatLossSingleDto transToPlatLossSingleDto(Claim2VsResponse claim2VsResponse, String lossId, String registNo,
            String lossUuid) throws Exception {
        PlatLossSingleDto platLossSingleDto = new PlatLossSingleDto();
        // 估损单相关信息
        EstimateInfo estimateInfo = claim2VsResponse.getDoWithEvaluateDocInfoResponseDto().getEstimateInfo();
        // 辅料信息
        List<GcEvaluateAccessory> gcEvaluateAccessory = claim2VsResponse.getDoWithEvaluateDocInfoResponseDto()
                .getGcEvaluateAccessory();
        // 修理信息
        List<GcEvaluateRepair> gcEvaluateRepair = claim2VsResponse.getDoWithEvaluateDocInfoResponseDto()
                .getGcEvaluateRepair();
        // 换件信息
        List<GcEvaluateFit> gcEvaluateFit = claim2VsResponse.getDoWithEvaluateDocInfoResponseDto().getGcEvaluateFit();

        // 定损单主表
        PrpmLossMain prpmLossMain = new PrpmLossMain();
        ClaimLossMainDto claimLossMainDto = new ClaimLossMainDto();
        if (estimateInfo != null && !"".equals(estimateInfo)) {
            if (estimateInfo.getRegistNo() != null && !"".equals(estimateInfo.getRegistNo())) {
                ConvertUtils.register(new DateLocaleConverter(null), Date.class);
                BeanUtils.copyProperties(claimLossMainDto, estimateInfo);
                claimLossMainDto.setDamageTime(DateUtils.convertToDate(estimateInfo.getDamageDate()));
                claimLossMainDto.setCaseNo(estimateInfo.getRegistNo());
                claimLossMainDto.setLossId(lossId);
                claimLossMainDto.setLossUuid(lossUuid);
                BeanUtils.copyProperties(prpmLossMain, claimLossMainDto);
                prpmLossMain.setDamageDate(claimLossMainDto.getDamageTime());
            }
        }
        // 辅料信息
        List<PrpmLossAccessory> prpmlossAccessoryList = new ArrayList<PrpmLossAccessory>();
        if (gcEvaluateAccessory != null && gcEvaluateAccessory.size() > 0) {
            for (int i = 0; i < gcEvaluateAccessory.size(); i++) {
                PrpmLossAccessory prpmLossAccessory = new PrpmLossAccessory();
                BeanUtils.copyProperties(prpmLossAccessory, gcEvaluateAccessory.get(i));
//                prpmLossAccessory.setLossUuid(lossUuid);
                prpmlossAccessoryList.add(prpmLossAccessory);

            }
        }
        // 修理信息
        List<PrpmLossRepair> prpmLossRepairList = new ArrayList<PrpmLossRepair>();
        if (gcEvaluateRepair != null && gcEvaluateRepair.size() > 0) {
            for (GcEvaluateRepair gcRepair : gcEvaluateRepair) {
                PrpmLossRepair prpmLossRepair = new PrpmLossRepair();
                BeanUtils.copyProperties(prpmLossRepair, gcRepair);
//                prpmLossRepair.setLossUuid(lossUuid);
                prpmLossRepairList.add(prpmLossRepair);
            }
        }
        // 换件信息
        List<PrpmLossFit> prpmLossFitList = new ArrayList<PrpmLossFit>();
        if (gcEvaluateFit != null && gcEvaluateFit.size() > 0) {
            for (GcEvaluateFit gcfit : gcEvaluateFit) {
                PrpmLossFit prpmLossFit = new PrpmLossFit();
                BeanUtils.copyProperties(prpmLossFit, gcfit);
//                prpmLossFit.setLossUuid(lossUuid);
                prpmLossFitList.add(prpmLossFit);
            }
        }

        platLossSingleDto.setLossAccessoryList(prpmlossAccessoryList);
        platLossSingleDto.setLossFitList(prpmLossFitList);
        platLossSingleDto.setLossRepair(prpmLossRepairList);
        platLossSingleDto.setLossMain(prpmLossMain);
        return platLossSingleDto;
    }
    
    public PlatLossSingleDto transToPlatLossSingleDto(EvaluateInfoResponseDto evaluateInfoResponseDto, String lossId, String lossUuid) throws Exception{
        
        PlatLossSingleDto platLossSingleDto = new PlatLossSingleDto();
        // 估损单相关信息
        com.liveneo.adapter.webservice.client.claim.EstimateInfo estimateInfo = evaluateInfoResponseDto.getEstimateInfo();
        // 辅料信息
        List<com.liveneo.adapter.webservice.client.claim.GcEvaluateAccessory> gcEvaluateAccessory = evaluateInfoResponseDto.getGcEvaluateAccessory();
        // 修理信息
        List<com.liveneo.adapter.webservice.client.claim.GcEvaluateRepair> gcEvaluateRepair = evaluateInfoResponseDto.getGcEvaluateRepair();
        // 换件信息
        List<com.liveneo.adapter.webservice.client.claim.GcEvaluateFit> gcEvaluateFit = evaluateInfoResponseDto.getGcEvaluateFit();
        // 定损单主表
        PrpmLossMain prpmLossMain = new PrpmLossMain();
        ClaimLossMainDto claimLossMainDto = new ClaimLossMainDto();
        if (estimateInfo != null && !"".equals(estimateInfo)) {
            if (estimateInfo.getRegistNo() != null && !"".equals(estimateInfo.getRegistNo())) {
                ConvertUtils.register(new DateLocaleConverter(null), Date.class);
                BeanUtils.copyProperties(claimLossMainDto, estimateInfo);
                claimLossMainDto.setDamageTime(estimateInfo.getDamageDate());
                claimLossMainDto.setCaseNo(estimateInfo.getRegistNo());
                claimLossMainDto.setLossId(lossId);
                claimLossMainDto.setLossUuid(lossUuid);
                BeanUtils.copyProperties(prpmLossMain, claimLossMainDto);
                prpmLossMain.setDamageDate(claimLossMainDto.getDamageTime());
            }
        }
        // 辅料信息
        List<PrpmLossAccessory> prpmlossAccessoryList = new ArrayList<PrpmLossAccessory>();
        if (gcEvaluateAccessory != null && gcEvaluateAccessory.size() > 0) {
            for (int i = 0; i < gcEvaluateAccessory.size(); i++) {
                PrpmLossAccessory prpmLossAccessory = new PrpmLossAccessory();
                BeanUtils.copyProperties(prpmLossAccessory, gcEvaluateAccessory.get(i));
                prpmLossAccessory.setLossUuid(lossUuid);
                prpmlossAccessoryList.add(prpmLossAccessory);

            }
        }
        // 修理信息
        List<PrpmLossRepair> prpmLossRepairList = new ArrayList<PrpmLossRepair>();
        if (gcEvaluateRepair != null && gcEvaluateRepair.size() > 0) {
            for (com.liveneo.adapter.webservice.client.claim.GcEvaluateRepair gcRepair : gcEvaluateRepair) {
                PrpmLossRepair prpmLossRepair = new PrpmLossRepair();
                BeanUtils.copyProperties(prpmLossRepair, gcRepair);
                prpmLossRepair.setLossUuid(lossUuid);
                prpmLossRepairList.add(prpmLossRepair);
            }
        }
        // 换件信息
        List<PrpmLossFit> prpmLossFitList = new ArrayList<PrpmLossFit>();
        if (gcEvaluateFit != null && gcEvaluateFit.size() > 0) {
            for (com.liveneo.adapter.webservice.client.claim.GcEvaluateFit gcfit : gcEvaluateFit) {
                PrpmLossFit prpmLossFit = new PrpmLossFit();
                BeanUtils.copyProperties(prpmLossFit, gcfit);
                prpmLossFit.setLossUuid(lossUuid);
                prpmLossFitList.add(prpmLossFit);
            }
        }

        platLossSingleDto.setLossAccessoryList(prpmlossAccessoryList);
        platLossSingleDto.setLossFitList(prpmLossFitList);
        platLossSingleDto.setLossRepair(prpmLossRepairList);
        platLossSingleDto.setLossMain(prpmLossMain);
        return platLossSingleDto;
    }

    /**
     * PolicyDto转换
     * 
     * @return
     */
    public PolicyDto turnsToPolicyDto(Claim2VsResponse claim2VsResponse, String caseUuid) throws Exception {
        PolicyDto policyDto = new PolicyDto();
        CaseInfoDto caseInfoDto = new CaseInfoDto();
        List<PolicyItemDto> policyItemDtoList = new ArrayList<PolicyItemDto>();
        CaseInfo caseInfo = claim2VsResponse.getCaseinfo();
        if (caseInfo != null && !"".equals(caseInfo)) {
            caseInfoDto.setCaseNo(caseInfo.getRegistNo());
            caseInfoDto.setDamagePlace(caseInfo.getDamagePlace());
            caseInfoDto.setDamageDate(caseInfo.getDamageDate());
            caseInfoDto.setLicenseNo(caseInfo.getLicenseNo());
            caseInfoDto.setUuid(caseUuid);

            List<PolicyInfo> policyList = caseInfo.getPolicyLst();
            if (policyList != null && policyList.size() > 0) {
                for (int i = 0; i < policyList.size(); i++) {
                    PolicyItemDto policyItemDto = new PolicyItemDto();
                    policyItemDto.setPolicyNo(policyList.get(i).getPolicyNo());
                    policyItemDto.setInsurantName(policyList.get(i).getInsuredName());
                    policyItemDto.setRiskName(policyList.get(i).getSpeciesName());
                    policyItemDto.setRiskClass(policyList.get(i).getCategoryName());
                    policyItemDto.setTbName(policyList.get(i).getAppliName());
                    policyItemDtoList.add(policyItemDto);
                }
            }

        }
        policyDto.setCaseInfo(caseInfoDto);
        policyDto.setPolicyItemDtoList(policyItemDtoList);
        return policyDto;
    }

}
